package com.xu.cloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SentinelController {

    // 普通接口
    @GetMapping("/hello")
    public String hello() {
        return "Hello Sentinel!";
    }

    // 限流接口
    @GetMapping("/test")
    @SentinelResource(value = "test", blockHandler = "handleBlock")
    public String test(@RequestParam(value = "param", required = false) String param) {
        return "Test API 调用成功: " + param;
    }

    // 限流处理方法
    public String handleBlock(String param, BlockException e) {
        return "请求被 Sentinel 限流: " + e.getMessage();
    }
}